package com.qn.wechat.service;

import com.jeesuite.confcenter.ConfigChangeHanlder;
import com.jeesuite.mybatis.plugin.pagination.Page;
import com.jeesuite.spring.ApplicationStartedListener;
import com.jeesuite.springweb.model.WrapperResponse;
import com.oneplatform.base.exception.AssertUtil;
import com.qn.wechat.common.MapperUtils;
import com.qn.wechat.dao.entity.ReplyContentConfigEntity;
import com.qn.wechat.dao.entity.ext.ReplyContentConfigEntityExt;
import com.qn.wechat.dao.mapper.ReplyContentConfigEntityMapper;
import com.qn.wechat.ifservice.dto.WXReplyConfigDto;
import com.qn.wechat.ifservice.dto.para.ReplyContentConfigPara;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@Service
public class ReplyContentConfigService implements ConfigChangeHanlder, ApplicationStartedListener {


    @Override
    public void onConfigChanged(Map<String, Object> changedConfigs) {

    }

    @Override
    public void onApplicationStarted(ApplicationContext applicationContext) {

    }


    @Resource
    ReplyContentConfigEntityMapper replayContentConfigEntityMapper;
    @Resource
    IdService idService;

    @Resource
    MapperUtils mapperUtils;

    public Page<WXReplyConfigDto> getList(@RequestBody ReplyContentConfigPara para) {

        Page<WXReplyConfigDto> response = new Page<WXReplyConfigDto>();

        Page<ReplyContentConfigEntityExt> list = replayContentConfigEntityMapper.findList(para, para);

        if (list != null)
            BeanUtils.copyProperties(list, response);

        return response;
    }


    public WrapperResponse<String> save(WXReplyConfigDto dto) {

        WrapperResponse<String> response = new WrapperResponse<>();
        AssertUtil.notBlank(dto.getReplyContent(),"回复内容不能为空");

        ReplyContentConfigEntity entity = new ReplyContentConfigEntity();
        BeanUtils.copyProperties(dto, entity);


        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(String.valueOf(idService.genId()));

            entity.setCreateTime(new Date());
            replayContentConfigEntityMapper.insert(entity);
        } else {
            replayContentConfigEntityMapper.updateByPrimaryKeySelective(entity);
        }

        response.setData(entity.getId());
        return response;
    }



}
